<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body style="background-image: url(jpg/14095097HcA.jpg);">
  <div class="row align-items-center">
    <div class="col"></div>
    <div class="col">
        <div class="alert alert-danger text-center invisible" role="alert" style="width: 42rem;background: rgba(156, 152, 152, 0.5)">
            <strong>提示：</strong>
            <font id="msgId" style="color: #000;">这里有一些错误，看着就好</font>
        </div>
    </div>
    <div class="col"></div>
</div>
    <div class="card" style="width: 50rem;margin: auto; margin-top: 10rem;height: 32rem;background: rgba(156, 152, 152, 0.5)">
      <form id="registerForm" action="/registerDone" method="POST">

        <div class="form-group">
            <label class="col-2" for="exampleInputEmail1">用户名：</label>
            <input type="text" class="form-control" name="username" id="username"
                aria-describedby="emailHelp"
                style="width: 35rem;background: rgba(156, 152, 152, 0.5);" placeholder="用户名">
            <small id="comment" class="form-text text-muted">{{msg}}</small>
        </div>
        <div class="form-group">
            <label class="col-2" for="exampleInputPassword1">密码：</label>
            <input type="password" class="form-control" name="password" id="password"
            style="width: 35rem;background: rgba(156, 152, 152, 0.5);" placeholder="密码">
        </div>
        <div class="form-group">
            <label class="col-2" for="exampleInputPassword1">确认密码：</label>
            <input type="password" class="form-control" name="confirmpassword"
                id="confirmpassword" style="width: 35rem;background: rgba(156, 152, 152, 0.5);" placeholder="确认密码">
        </div>

        <button type="button" class="btn btn-primary" style="background: rgba(156, 152, 152, 0.5);width: 7rem;"
            onclick="register()">注册</button>
        <button type="button" class="btn btn-primary" style="background: rgba(156, 152, 152, 0.5);width: 7rem;"
            onclick="console.log(alert('退出成功'));cancel()">取消</button>
    </form>
      </div>
      <script src="js/bootstrap.min.js"></script>
      <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
      <script src="js/jquery-3.6.0.min.js"></script>
      <script>
        function register() {
            // 获取表单数据
            let uid = $('#username').val();
            let pwd = $('#password').val();
            let confirmpwd = $('#confirmpassword').val();

            // 判断用户名、密码是否为空，为空则提示；不为空则向服务器提交
            if (uid.length > 0 && pwd.length > 0 && pwd === confirmpwd) {
                axios({
                    method: 'post',
                    url: '/registerDone',
                    data: $('#registerForm').serialize(),
                }).then(function (res) {
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res = res.data;
                    if (res.code === 200) {
                        window.location.href = '/login';
                    } else {
                        alert_fn(res.msg);
                        console.log(res.msg);
                    }
                }).catch((err) => {
                    alert_fn(err);
                    console.log(err);
                });
            } else {
                alert_fn('用户名、密码不能为空，并且两次密码应该一致');
            }
        }

           function cancel(){
             window.location.href='/login';
           };

           function alert_fn(msg) {
            $('#msgId').text(msg);
            $('.alert').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }

      </script>
</body>
</html>